/**
 * 
 */
package info.unlp.tesis.transformation.plugin.xmi;

import info.unlp.tesis.transformation.plugin.wizard.GenerateModelInstancePage;
import info.unlp.tesis.transformation.plugin.wizard.GenerateXMIPage;

import java.util.HashMap;

import org.eclipse.core.resources.IFile;
import org.eclipse.emf.mwe.core.WorkflowRunner;

/**
 * @author Silvana Mossi
 * 
 */
public class GenerateXMIFactory {

	/**
	 * Pagina que invoca al factory
	 */
	private GenerateXMIPage page;

	/**
	 * Constructor
	 * @param generateXMIPage pagina que invoca al factory
	 */
	public GenerateXMIFactory(final GenerateXMIPage generateXMIPage) {
		this.page=generateXMIPage;
	}

	/**
	 */
	public boolean createXMI() {

		IFile fileQVT = ((GenerateModelInstancePage)this.getPage().getPreviousPage()).getFileQVT();

		String modelFilePath = fileQVT.getLocationURI().toString();
		HashMap<String, String> properties = new HashMap<String, String>();
		properties.put("modelFile", modelFilePath);
		String path = this.getResourcePath(modelFilePath);
		properties.put("targetFile", path + "/qvtoperational_model.xmi");
		
		//ejecuto el mwe
		WorkflowRunner runner = new WorkflowRunner();
		return  runner
				.run(
						"/src/info/unlp/tesis/transformation/plugin/xtext/mwe/generator/TransformationGenerator.mwe",
						null, properties, null);
	}

	/**
	 * Obtiene a partir del archivo seleccionado su path de ubicacion.
	 * 
	 * @param modelFilePath
	 * @return camino de ubicacion del archivo.
	 */
	private String getResourcePath(final String modelFilePath) {
		int partLast = modelFilePath.lastIndexOf("/");
		return modelFilePath.substring(0, partLast);
	}

	/**
	 * Getter for page.
	 * @return the page
	 */
	public GenerateXMIPage getPage() {
		return page;
	}

	/**
	 * Setter for page.
	 * @param page the page to set
	 */
	public void setPage(GenerateXMIPage page) {
		this.page = page;
	}

}
